#type=0 表示买家未登录， type=1 表示买家已登录
#这里假设买家已经登录了
import sys
import xlrd
import xlwt
import os
from xlutils.copy import copy
sys.path.append('D:\\python1\\ebsystem')
from ulits.excelclass import Excel
p=os.path.dirname(__file__)
parent_path = os.path.dirname(p)
print(parent_path)

class Shoping_cart(object):
    def __init__(self,table_name,sheet_name,func):
        self.table=table_name
        self.sheet=sheet_name
        self.func=func
        new1={}#购物表的读取结果，列表形式返回
        self.new1=new1
    def check_goods(self):#查看商品，包括添加商品到购物车
        # ww=xlrd.open_workbook(self.table)
        # sheet=ww.sheet_by_name(self.sheet)
        file_path = parent_path + '/data/goods_info.xls'
        excel=Excel(file_path)
        excel.find_data('商品信息')
        #自己写的查看商品
        # new={}
        # for i in range(sheet.nrows):
        #     goods_list=[]
        #     dict_goods={}
        #     for j in range(sheet.ncols):
        #         goods_list.append(sheet.cell(i,j).value)
        #     del goods_list[0]
        #     dict_goods[sheet.cell(i,0).value]=goods_list
        #     new.update(dict_goods)
        # print(new)
        add=input('您可以输入您喜欢的产品编号加入购物车，返回上一级请输入Q/q:')
        ww=xlrd.open_workbook(file_path)
        sheet=ww.sheet_by_name('商品信息')
        
        #print(l1)
        
        if add.isdigit():
            l1=sheet.col_values(0)
            add=int(add)
            if add in l1:
                number1=input('请输入您要购买的数量：').strip()
                
                if number1.isdigit():
                    number1=int(number1)
                    l2=sheet.row_values(add+1)
                    print(l2)
                    print(l2[3])
                    if number1<=l2[3]:
                        print('可以购买！')
                    else:
                        print('库存不足！')
                    
                #     if number1<=new[add][3]:
                #         new[add][3]=new[add][3]-number1
                #         #修改商品表的库存
                #         new_table1=copy(ww)
                #         new_sheet1=new_table1.get_sheet(0)
                #         new_sheet1.write(add+1,4,new[add][3])
                #         new_table1.save(self.table)
                        
                #         id1=input('请输入买家id:').strip()
                #         #把购买的商品信息存入购物车表
                #         ww1=xlrd.open_workbook('D:\\购物车系统表.xls')
                #         sheet1=ww1.sheet_by_name('购物车表')
                #         lq=[]
                #         ww2=copy(ww1)
                #         new_sheet1=ww2.get_sheet(1)
                #         for m in range(sheet1.nrows):
                #             list1=[]
                #             for n in range(sheet1.ncols):
                #                 list1.append(sheet1.cell(m,n).value)
                #             lq.append(list1)
                #         list2=[]
                #         list2.append(add)
                #         list2.append(new[add][0])
                #         list2.append(new[add][1])
                #         list2.append(new[add][2])
                #         list2.append(number1)
                #         list2.append(id1)
                #         lq.append(list2)
                #         for m in range(len(lq)):
                #             for n in range(len(lq[m])):
                #                 new_sheet1.write(m,n,lq[m][n])
                #         ww2.save('D:\\购物车系统表.xls')
                #         print('购买成功')
                #     else:
                #         print('库存不足！')

                else:
                    print('输入有误！')
            else:
                print('当前商品不存在')
        elif add.upper()=='Q':
            self.vive()
        else:
            print('您的输入有误')

    def check_shoping_car(self):#查看购物车，包括删除指定商品
        
        ww1=xlrd.open_workbook('D:\\购物车系统表.xls')
        sheet1=ww1.sheet_by_name('购物车表')
        file_path = parent_path + '/data/shopping_car.xls'
        excel=Excel(file_path)
        excel.find_data('购物车')
        #自己写的查看购物车表
        # for i in range(sheet1.nrows):
        #     goods_list=[]
        #     dict_goods={}
        #     for j in range(sheet1.ncols):
        #         goods_list.append(sheet1.cell(i,j).value)
        #     del goods_list[0]
        #     dict_goods[sheet1.cell(i,0).value]=goods_list
        #     self.new1.update(dict_goods)
        # print(self.new1)
        input1=input('请输入您要删除的商品id,Q/q删除：')
        if input1.isdigit():
            input1=int(input1)
            if input1 in self.new1.keys():
                
                del self.new1[input1]
                print(self.new1)
                print('可以删除')
                #把删除过的new1重新写入购物车表
            else:
                print('暂无该商品不能删除！')
            
        elif input1.upper()=='Q':
            self.vive()
        else:
            print('输入有误！')



        
       
    def settle_accounts(self):#结算,生成订单#生成订单功能还没做，注意应该有一个下单时间
        number2=input('您是否要进行结算n/N结算，q/Q删除：')
        
        if number2.upper()=='N':
            ll=[]
            sum=0
            for i in self.new1.keys():
                ll.append(i)
            ll=ll [1:len(ll)]
            for k in ll:
                sum+=self.new1[k][2]*self.new1[k][3]
            print('您要购买的商品总价为{}元：'.format(sum))
        elif number2.upper()=='Q':
            self.vive()
        else:
            print('输入有误')

    def empty(self):#清空购物车
        titles=['商品id','卖家id','商品名称','商品价格','购买数量','买家id']
        ww1=xlrd.open_workbook('D:\\购物车系统表.xls')
        sheet1=ww1.sheet_by_name('购物车表')
        new1=copy(sheet1)
        sheet2=new1.get_sheet(1)
        for i in range(len(titles)):
            sheet2.write(0,i,titles[i])
        new1.save('D:\\购物车系统表.xls')
    def vive(self):
        while True:
            print('欢迎进入电销平台系统'.center(80,'*'))
            for i,j in enumerate(self.func,1):
                print(i,j)
            number=input('请输入您要选择的功能编号q/Q退出该系统:')
            if number.isdigit():
                number=int(number)
                if number==1:
                    self.check_goods()
                if number==2:
                    self.check_shoping_car()
                if number==3:
                    self.settle_accounts()

            elif number.upper()=='Q':
                exit()
            else:
                print('输入有误请重新输入')   


        





func_list=['查看商品','查看购物车','结算','清空购物车']
shoping_cart=Shoping_cart('D:\\购物车系统表.xls','商品表',func_list)
# shoping_cart.check_goods()
shoping_cart.vive()
